package com.ruoyi.mall.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.domain.bo.CustomerCartBo;
import com.ruoyi.mall.domain.vo.CustomerCartVo;
import com.ruoyi.mall.domain.CustomerCart;
import com.ruoyi.mall.mapper.CustomerCartMapper;
import com.ruoyi.mall.service.ICustomerCartService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 会员购物车Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-12
 */
@RequiredArgsConstructor
@Service
public class CustomerCartServiceImpl implements ICustomerCartService {

    private final CustomerCartMapper baseMapper;

    /**
     * 查询会员购物车
     */
    @Override
    public CustomerCartVo queryById(Long customerCartId){
        return baseMapper.selectVoById(customerCartId);
    }

    @Override
    public List<CustomerCartVo> queryCartByCustomerId(Long customerId)
    {
        LambdaQueryWrapper<CustomerCart> lqw = Wrappers.lambdaQuery();
        lqw.eq(customerId != null, CustomerCart::getCustomerId, customerId);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 查询会员购物车列表
     */
    @Override
    public TableDataInfo<CustomerCartVo> queryPageList(CustomerCartBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CustomerCart> lqw = buildQueryWrapper(bo);
        Page<CustomerCartVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询会员购物车列表
     */
    @Override
    public List<CustomerCartVo> queryList(CustomerCartBo bo) {
        LambdaQueryWrapper<CustomerCart> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CustomerCart> buildQueryWrapper(CustomerCartBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CustomerCart> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getGoodsId() != null, CustomerCart::getGoodsId, bo.getGoodsId());
        lqw.eq(bo.getCustomerId() != null, CustomerCart::getCustomerId, bo.getCustomerId());
        lqw.eq(bo.getGoodsMoney() != null, CustomerCart::getGoodsMoney, bo.getGoodsMoney());
        lqw.eq(bo.getGoodsCount() != null, CustomerCart::getGoodsCount, bo.getGoodsCount());
        return lqw;
    }

    /**
     * 新增会员购物车
     */
    @Override
    public Boolean insertByBo(CustomerCartBo bo) {
        CustomerCart add = BeanUtil.toBean(bo, CustomerCart.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCustomerCartId(add.getCustomerCartId());
        }
        return flag;
    }

    /**
     * 修改会员购物车
     */
    @Override
    public Boolean updateByBo(CustomerCartBo bo) {
        CustomerCart update = BeanUtil.toBean(bo, CustomerCart.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CustomerCart entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除会员购物车
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
